import commonFieldLib from '@/widgets/helper/setting/common'
import commonTableFieldLib from '@/widgets/helper/setting/tableCommon'
import dataFieldLib from '@/widgets/helper/setting/data'
import commonTabsFieldLib from '@/widgets/helper/setting/tabs'
import modules from '@/widgets/helper/setting'
import _ from 'lodash'

const getters = {
  /**
   * 当前制作的表单实体
   * @param state
   */
  model(state) {
    const m = {}
    console.log(state.fields,'====----00000000000')
    if(state.fields.length>0){
      state.fields[state.tab_id]['content'].forEach(item => {
        if (!item) return
        // 分隔线组件无值
        if (item.type !== 'divider') {
          m[item.prop] = (item.props || {}).value || item.value
        }
      })
    }

    return m
  },
  /**
   * 通用数据属性表单字段配置
   * @param state
   * @return {Array|*}
   */
  dataFields(state) {
    console.log(dataFieldLib, 444444444)
    if (!state.editField) return []
    //  公共属性设置表单的字段集合
    let fields = [...dataFieldLib]


    // 无选项的字段，不支持字典设置
    if (!state.editField.options) {
      fields = fields.filter(n => n.prop !== 'dict' && n.prop !== 'dictapi')
    } else {
      var field = fields.filter(n => {
        if (n.prop == 'dictapi') {
          var dictmap = {
            ...modules['dict']
          }

          const options = _.keys(dictmap).map(ns => {
            return {
              label: '接口_' + ns,
              value: ns,
            }
          })

          var finsti = _.defaultsDeep(n, {
            options: options
          })
          return finsti;
        }
      })
    }
    return fields
  },


  /**
   * 通用数据属性表单实体
   * @param state
   * @param getters
   * @return {{}}
   */
  dataModel(state, getters) {
    if (!getters.dataFields) return {}
    const model = {}
    getters.dataFields.forEach(item => {
      model[item.prop] = state.editField[item.prop]
    })
    console.log(model);
    return model
  },


  /**
   * 通用属性表单字段配置
   * @param state
   * @return {Array|*}
   */
  commonFields(state) {
    console.log(commonFieldLib, state.tab_id, 444444444)
    if (!state.editField) return []
    // 分隔线组件无表单字段通用属性
    if (state.editField.type === 'divider') {
      return []
    }

    //  公共属性设置表单的字段集合
    let fields = [...commonFieldLib]


    // 无选项的字段，不支持字典设置
    if (!state.editField.options) {
      fields = fields.filter(n => n.prop !== 'dict' && n.prop !== 'dictapi')
    } else {
      var field = fields.filter(n => {
        if (n.prop == 'dictapi') {
          var dictmap = {
            ...modules['dict']
          }

          const options = _.keys(dictmap).map(ns => {
            return {
              label: '接口_' + ns,
              value: ns,
            }
          })

          var finsti = _.defaultsDeep(n, {
            options: options
          })
          return finsti;
        }
      })
    }
    // 计算可以依赖的字段集合
    const canDependFields = state.fields[state.tab_id]['content'].filter(n => n.prop !== state.editField.prop)
console.log(canDependFields,state.tab_id,'11111111111111======')
    // console.log(canDependFields,state.fields,'================================================================')
    // 把可依赖的字段集合构造成下拉选择框的选项
    const options = canDependFields.map(n => {
      return {
        label: n.prop,
        value: n.prop,
        type: n.type
      }
    })

    fields.push({
      label: '依赖字段',
      type: 'select',
      prop: 'depend',
      options: options,
      props: {
        clearable: true
      }
    })

    // 当前编辑的字段依赖字段名称
    const dependProp = state.editField.depend
    if (dependProp) {
      // 找到依赖字段的输入框类型
      const dependType = (options.find(n => n.value === dependProp) || {
        type: 'text'
      }).type
      // 找到对应组件的参数属性集合
      const dependComponentFields = state.props[dependType]
     
      // 从参数集合中找到value的的配置，目的是为了找到value的数据类型，根据数据类型匹配对应的输入框
      const valueField = dependComponentFields.find(n => n.prop === 'value')
   
      if (valueField) {
        fields.push({
          label: '依赖值',
          prop: 'dependValue',
          type: valueField.type
        })
      }

    }
    return fields
  },

  //-------------表格开始--------------

  /**
   * 当前制作的表格实体
   * @param state
   */
  tableModel(state) {
    const m = {}
    state.tableField.forEach(item => {
      if (!item) return
      // 分隔线组件无值
      if (item.type !== 'divider') {
        m[item.prop] = (item.props || {}).value || item.value
      }
    })
    console.log(m, 'mmmmmmmmmmmmmmmmmmmmmmmmm')
    return m
  },
  /**
   * 通用属性表格字段配置
   * @param state
   * @return {Array|*}
   */
  commonTableFields(state) {
    console.log(commonTableFieldLib, state.tab_id, 444444444)
    console.log(state.tableEditField , state.ButtonEditField, 444444444)
    if (!state.tableEditField && !state.ButtonEditField) return []
    // 分隔线组件无表单字段通用属性
    if (state.tableEditField && state.tableEditField.type === 'divider') {
      return []
    }
    //  公共属性设置表单的字段集合
    let fields = [...commonTableFieldLib]
    return fields
  },

  /**
   * 通用属性表格实体
   * @param state
   * @param getters
   * @return {{}}
   */
  commonTableModel(state, getters) {
    console.log(getters.commonTableFields,state.editType, state.ButtonEditField,'444444444================================================================')
    if (!getters.commonTableFields) return {}
    const model = {}
    getters.commonTableFields.forEach(item => {
      model[item.prop] = state.editType['type'] == 'table' ? state.tableEditField[item.prop] : state.ButtonEditField[item.prop]
    })
    console.log(model);
    return model
  },

  /**
   * 扩展属性表格实体
   * @param state
   * @param getters
   * @return {{}}
   */
  extendTableModel(state, getters) {
    console.log(getters, '00000000000')
    if (!getters.extendTableFields) return {}
    const model = {}
    // const models = []
    // const objs={}

    getters.extendTableFields.forEach(item => {
      // console.log(item);
      // if (state.tableEditField.children) {
      //   state.tableEditField.children.forEach((items, index) => {
      //     const value = items.props[item.prop];
      //     console.log(items,'+++================================================================',index)
      //     if (typeof value === 'undefined') {
      //       const props = state.tableProps[items.type] || []
      //       const defaultValues = {}
      //       props.forEach(n => {
      //         defaultValues[n.prop] = n.value
      //       })
      //       // objs[item.prop] = defaultValues[item.prop]
      //       // models[index]=objs;
      //     }else{
      //       objs[item.prop] = value
      //       models[index]=objs;
      //     }
      //     console.log(objs,'-----========================-----',index)

      //   })
      //   // console.log(objs)
      //   model = models
      //   // model[item.prop] =models;
      // } else {
      //   const value = state.tableEditField.props[item.prop]
      //   console.log(value,'+++================================================================')
      //   model[item.prop] = typeof value === 'undefined' ? getters.defaultTableValue[item.prop] : value
      // }

      const value = state.editType['type'] == 'table' ? state.tableEditField.props[item.prop] : state.ButtonEditField.props[item.prop]
      // console.log(value,'+++================================================================')
      model[item.prop] = typeof value === 'undefined' ? getters.defaultTableValue[item.prop] : value
    })
    // console.log(model, '表格的model')
    return model
  },

  /**
   * 扩展属性表格字段配置
   * @param state
   * @return {Array|*}
   */
  extendTableFields(state) {

    if (!state.tableEditField && !state.ButtonEditField) return []
    console.log(state.tableEditField,state.ButtonEditField,state.editType,'fdsafdsafdsafdsafdsafdsafdsafdsafdas')
    if(state.tableEditField && state.editType['type'] == 'table'){
      return state.tableProps[state.tableEditField.type];
    }else if (state.ButtonEditField && state.editType['type'] == 'button'){
      return state.tableProps[state.ButtonEditField.type];
    }
  },


  /**
   * 计算表格属性默认值
   * @param state
   * @param getters
   * @return {{}}
   */
  defaultTableValue(state, getters) {
    if (!state.tableEditField && !state.ButtonEditField) return {}
    const type = state.editType['type'] == 'table' ? state.tableEditField.type : state.ButtonEditField.type
    const props = state.tableProps[type] || []
    const defaultValues = {}
    props.forEach(n => {
      defaultValues[n.prop] = n.value
    })
    return defaultValues
  },

  /**
   * 扩展属性表单字段配置
   * @param state
   * @return {Array|*}
   */
  extendFields(state) {
    if (!state.editField) return []
    return state.props[state.editField.type]
  },

  /**
   * 通用属性表单实体
   * @param state
   * @param getters
   * @return {{}}
   */
  commonModel(state, getters) {
    if (!getters.commonFields) return {}
    const model = {}
    getters.commonFields.forEach(item => {
      model[item.prop] = state.editField[item.prop]
    })
    console.log(model);
    return model
  },



  /**
   * 通用属性表单字段配置
   * @param state
   * @return {Array|*}
   */
  commonTabsFields(state) {
    if (!state.tabsEditField) return []

    //  公共属性设置表单的字段集合
    let fields = [...commonTabsFieldLib]

    return fields
  },
  /**
   * 通用属性表单实体
   * @param state
   * @param getters
   * @return {{}}
   */
  commonTabsModel(state, getters) {
    if (!getters.commonTabsFields) return {}
    const model = {}

    getters.commonTabsFields.forEach(item => {
      model[item.prop] = state.tabsEditField[item.prop]
    })
    console.log(model);
    return model
  },

  /**
   * 计算默认值
   * @param state
   * @param getters
   * @return {{}}
   */
  defaultValue(state, getters) {
    if (!state.editField) return {}
    const type = state.editField.type
    const props = state.props[type] || []
    const defaultValues = {}
    props.forEach(n => {
      defaultValues[n.prop] = n.value
    })
    return defaultValues
  },

  /**
   * 扩展属性表单实体
   * @param state
   * @param getters
   * @return {{}}
   */
  extendModel(state, getters) {
    console.log(getters, '00000000000')
    if (!getters.extendFields) return {}
    const model = {}

    getters.extendFields.forEach(item => {
      const value = state.editField.props[item.prop]
      model[item.prop] = typeof value === 'undefined' ? getters.defaultValue[item.prop] : value
    })
    return model
  },


  sidebar: state => state.app.sidebar,
  device: state => state.app.device,
  viewOption: state => state.app.viewOption,
  token: state => state.user.token,
  avatar: state => state.user.avatar,
  name: state => state.user.name,
  nodeInfo: state => state.user.nodeInfo,
  realIp: state => state.user.realIp,
  userInfo:state => state.user.userInfo,
  routers: state => state.user.routers,
  addRouters: state => state.user.addRouters,
  routerRules: state => state.user.routerRules,
  routerMenu : state => state.user.routerMenu,
  newUserArr : state => state.user.newUserArr,
  newOrderArr : state => state.user.newOrderArr,
  reserveNewOrderArr : state => state.user.reserveNewOrderArr,
  socketMessage : state => state.socket.socket.message,
}
export default getters
